package com.vitaf.cash.app.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.vitaf.cash.app.R
import com.vitaf.cash.app.databinding.VitafActPermissionsChildItemBinding
import com.vitaf.cash.lib.adapter.VitafRecyclerViewAdapter
import com.vitaf.cash.lib.adapter.VitafViewHolderHelper
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class VitafPermissionApt(recyclerView: RecyclerView?) : VitafRecyclerViewAdapter<JSONObject>(recyclerView,R.layout.vitaf_act_permissions_item) {

    override fun fillData(helper: VitafViewHolderHelper, position: Int, model: JSONObject) {
        val imgPermissionIcon = helper.getImageView(R.id.imgPermissionIcon)
        imgPermissionIcon.setImageResource(model.optInt("icon"))

        helper.setText(R.id.textPermissionTitle, model.optString("title"))

        val loanAmount = model.optString("loanAmount","")
        if (loanAmount.isNotEmpty()){
            helper.setText(R.id.textLoanAmount,"+${loanAmount}")
            helper.setVisibility(R.id.amountLayout,View.VISIBLE)
        } else {
            helper.setVisibility(R.id.amountLayout,View.GONE)
        }

        val passRate = model.optString("passRate","")
        if (passRate.isNotEmpty()){
            helper.setText(R.id.textPassRate,"+${passRate}"+"%")
            helper.setVisibility(R.id.passRateLayout,View.VISIBLE)
        } else {
            helper.setVisibility(R.id.passRateLayout,View.GONE)
        }
        if (loanAmount.isEmpty() || passRate.isEmpty()){
            helper.setVisibility(R.id.lineView, View.GONE)
        } else {
            helper.setVisibility(R.id.lineView, View.VISIBLE)
        }

        val childItemLayout = helper.getView<LinearLayout>(R.id.permissionChildLayout)
        childItemLayout.removeAllViews()
        val itemList = model.optJSONArray("msgList")
        itemList?.let {
            for (i in 0..< itemList.length()){
                val item = itemList.optJSONObject(i)
                val itemView = VitafActPermissionsChildItemBinding.inflate(LayoutInflater.from(mContext))
                val showTitle = item.optString("title")
                if (showTitle.isNotEmpty()){
                    itemView.textTitle.text = showTitle
                    itemView.textTitle.visibility = View.VISIBLE
                } else {
                    itemView.textTitle.visibility = View.GONE
                }
                itemView.textReason.text = item.optString("des")
                childItemLayout.addView(itemView.root)
            }
        }
    }
}